package com.shirotest.utility;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;

/**
 * 请求对象工具类
 */
public class RequestUtility {
	/**
	 * 从请求对象中以字符流的形式读取请求参数的字符串形式<br/>
	 * 编码方式为UTF-8格式<br/>
	 * 如果读取失败则返回空（null）<br/>
	 */
	public static String readStreamParameter(HttpServletRequest request){
 		StringBuilder buffer = new StringBuilder();
 		BufferedReader reader = null;
 		try {
 			ServletInputStream inputStream = request.getInputStream();
 			reader = new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));
 			String line = reader.readLine();
 			while (line != null) {
 				buffer.append(line);
 				line = reader.readLine();
 			}
 		} catch (Exception e) {
 			return null;
 		} finally {
 			if (null != reader) {
 				try {
 					reader.close();
 				} catch (Exception e) {
 				}
 			}
 		}
 		return buffer.toString();
 	}
}
